// 下载球队徽标的脚本
const fs = require('fs');
const https = require('https');
const path = require('path');

// 确保images目录存在
const imagesDir = path.join(__dirname, 'images');
if (!fs.existsSync(imagesDir)) {
    fs.mkdirSync(imagesDir);
}

// 球队徽标URL
const teamLogos = {
    'mancity': 'https://upload.wikimedia.org/wikipedia/zh/thumb/e/eb/Manchester_City_FC_badge.svg/200px-Manchester_City_FC_badge.svg.png',
    'realmadrid': 'https://upload.wikimedia.org/wikipedia/zh/thumb/c/c7/Logo_Real_Madrid.svg/200px-Logo_Real_Madrid.svg.png',
    'bayern': 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/FC_Bayern_M%C3%BCnchen_logo_%282017%29.svg/200px-FC_Bayern_M%C3%BCnchen_logo_%282017%29.svg.png',
    'psg': 'https://upload.wikimedia.org/wikipedia/zh/thumb/a/a7/Paris_Saint-Germain_F.C..svg/200px-Paris_Saint-Germain_F.C..svg.png',
    'barcelona': 'https://upload.wikimedia.org/wikipedia/zh/thumb/4/47/FC_Barcelona_%28crest%29.svg/200px-FC_Barcelona_%28crest%29.svg.png',
    'dortmund': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/Borussia_Dortmund_logo.svg/200px-Borussia_Dortmund_logo.svg.png',
    'inter': 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/FC_Internazionale_Milano_2021.svg/200px-FC_Internazionale_Milano_2021.svg.png',
    'arsenal': 'https://upload.wikimedia.org/wikipedia/zh/thumb/5/53/Arsenal_FC.svg/200px-Arsenal_FC.svg.png',
    'placeholder': 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Placeholder_view_vector.svg/200px-Placeholder_view_vector.svg.png'
};

// 下载图片函数
function downloadImage(url, filepath) {
    return new Promise((resolve, reject) => {
        https.get(url, (response) => {
            if (response.statusCode !== 200) {
                reject(new Error(`Failed to download: ${response.statusCode}`));
                return;
            }

            const fileStream = fs.createWriteStream(filepath);
            response.pipe(fileStream);

            fileStream.on('finish', () => {
                fileStream.close();
                console.log(`Downloaded: ${filepath}`);
                resolve();
            });

            fileStream.on('error', (err) => {
                fs.unlink(filepath, () => {}); // 删除不完整的文件
                reject(err);
            });
        }).on('error', (err) => {
            reject(err);
        });
    });
}

// 下载所有球队徽标
async function downloadAllLogos() {
    const promises = [];
    
    for (const [team, url] of Object.entries(teamLogos)) {
        const filepath = path.join(imagesDir, `${team}.png`);
        promises.push(downloadImage(url, filepath));
    }
    
    try {
        await Promise.all(promises);
        console.log('All logos downloaded successfully!');
    } catch (error) {
        console.error('Error downloading logos:', error);
    }
}

// 执行下载
downloadAllLogos(); 